package com.snap.vseries.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.bean.ProgramSection;

import java.util.Locale;

import me.jessyan.autosize.utils.AutoSizeUtils;

@SuppressLint("ViewConstructor")
public class SectionView extends ViewGroup {
    private TextView section;
    private TextView contentTv;
    private ProgramSection programSection;

    public SectionView(Context context, ProgramSection programSection) {
        super(context);
        this.programSection = programSection;
        setWillNotDraw(false);
        //温度和时间 50 ℃ / 120 s
        contentTv = new TextView(context);
        contentTv.setId(R.id.section_temp_tv);
        contentTv.setText(String.format(Locale.CHINA, "%s°C / %ds", programSection.getTemp(), programSection.getDuration()));
        contentTv.setTextColor(0xFF666666);
        contentTv.setTextSize(10);
        contentTv.setGravity(Gravity.CENTER_HORIZONTAL);
        LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        contentTv.setLayoutParams(params);
        addView(contentTv);
        //主体
        section = new TextView(context);
        section.setId(R.id.section_temp_tv);
        if ((programSection.getSectionIndex() - 1) % 4 > 1) {
            section.setBackground(ContextCompat.getDrawable(context, R.drawable.ba_stage2));
        } else {
            section.setBackground(ContextCompat.getDrawable(context, R.drawable.ba_stage1));
        }
        addView(section);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                LayoutParams lp = child.getLayoutParams();

                int childWidthSpec = getChildMeasureSpec(widthMeasureSpec, child.getPaddingLeft() + child.getPaddingRight(), lp.width);
                int childHeightSpec = getChildMeasureSpec(heightMeasureSpec, child.getPaddingTop() + child.getPaddingBottom(), lp.height);

                child.measure(childWidthSpec, childHeightSpec);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //温度最高可达120度，每30度为一个区间，共有四个区间。
        //section的左上的Y坐标
        int baseY = (int) ((120f - programSection.getTemp()) / 120f * b);
        int leftX = AutoSizeUtils.dp2px(getContext(), 1);
        int rightX = getWidth() - AutoSizeUtils.dp2px(getContext(), 1);
        section.layout(leftX, baseY, rightX, getHeight());
        //温度的位置
        contentTv.layout(leftX, baseY - contentTv.getMeasuredHeight(), rightX, baseY);
    }

    public ProgramSection getProgramSection() {
        return programSection;
    }


}
